1 // Accepted - Little tricks to try to make it faster. Doesn't really work.
24 #define foreach(x, v) for (typeof (v).begin() x=(v).begin(); x !=(v).end(); ++x)
25 #define For(i, a, b) for (int i=(a); i<(b); ++i)
26 #define D(x) cout << #x " is " << x << endl
29 #define MAXBUFF (1<<18)
30 char inBuffer
[MAXBUFF
], *pIn
= inBuffer
+MAXBUFF
;
31 char outBuffer
[MAXBUFF
], *pOut
= outBuffer
;
33 inline char read_char() {
34 if( pIn
== inBuffer
+MAXBUFF
) {
35 fread( inBuffer
, 1, MAXBUFF
, stdin
);
41 inline int read_int() {
43 while( !isdigit(c
= read_char()) );
45 while( isdigit(c
= read_char()) ) ret
= (ret
<< 3) + (ret
<< 1) + c
- '0';
50 fwrite( outBuffer
, 1, pOut
-outBuffer
, stdout
);
54 inline void write( char c
) {
55 if( pOut
== outBuffer
+MAXBUFF
) {
56 fwrite( outBuffer
, 1, MAXBUFF
, stdout
);
67 char mat
[MAXN
+1][MAXN
+1];
69 short r1
[MAXK
* 32], r2
[MAXK
* 32], c1
[MAXK
* 32], c2
[MAXK
* 32];
71 vector
<int> regionsAt
[MAXN
+1][MAXN
+1];
75 inline void fillRegion(int r
, char letter
) {
76 for (int i
= r1
[r
]; i
<= r2
[r
]; ++i
) {
77 for (int j
= c1
[r
]; j
<= c2
[r
]; ++j
) {
83 inline bool isEmpty(int r
) {
84 for (int i
= r1
[r
]; i
<= r2
[r
]; ++i
) {
85 for (int j
= c1
[r
]; j
<= c2
[r
]; ++j
) {
86 if (mat
[i
][j
] != '.') return false;
92 bool backtrack(int cell
, char face
) {
96 if (cr
>= N
or cc
>= N
) {
97 for (int i
= 0; i
< N
; ++i
) {
98 for (int j
= 0; j
< N
; ++j
) {
106 if (mat
[cr
][cc
] != '.') return backtrack(cell
+ 1, face
);
108 for (int k
= 0, m
= regionsAt
[cr
][cc
].size(); k
< m
; ++k
) {
109 int r
= regionsAt
[cr
][cc
][k
];
110 if (!isEmpty(r
)) continue;
112 if (backtrack(cell
+ 1, face
+ 1)) return true;
122 if (N
== 0 and K
== 0) break;
124 for (int i
= 0; i
< N
; ++i
) {
125 for (int j
= 0; j
< N
; ++j
) {
126 char c
= IO::read_char(); while (isspace(c
)) c
= IO::read_char();
128 regionsAt
[i
][j
].clear();
134 for (int i
= 0; i
< N
; ++i
) {
135 for (int j
= 0; j
< N
; ++j
) {
136 if (mat
[i
][j
] == '.') continue;
137 int size
= mat
[i
][j
] - '0';
139 for (int width
= 1; width
<= size
; ++width
) {
140 if (size
% width
!= 0) continue;
141 int height
= size
/ width
;
142 for (int ii
= max(0, i
- height
+ 1); ii
+ height
- 1 < N
and ii
<= i
; ++ii
) {
143 for (int jj
= max(0, j
- width
+ 1); jj
+ width
- 1 < N
and jj
<= j
; ++jj
) {
144 r1
[totalRegions
] = ii
;
145 r2
[totalRegions
] = ii
+ height
- 1;
146 c1
[totalRegions
] = jj
;
147 c2
[totalRegions
] = jj
+ width
- 1;
148 if (isEmpty(totalRegions
)) {
149 regionsAt
[ii
][jj
].push_back( totalRegions
);
155 mat
[i
][j
] = '0' + size
;
159 for (int i
= 0; i
< N
; ++i
) {
160 for (int j
= 0; j
< N
; ++j
) {